package beautiful.butterfly.drds.datasource.connection_contexts.spi.thread;

//import beautiful.butterfly.drds.datasource.author.Author;
//import beautiful.butterfly.drds.datasource.author.Authors;

import beautiful.butterfly.drds.datasource.connection_contexts.spi.ConnectionContexts;

//@Author(name = Authors.LI_YANG)
public class DestroyConnectionContextThread extends Thread {

    private ConnectionContexts connectionContexts;

    public DestroyConnectionContextThread(ConnectionContexts connectionContexts, String threadName) {
        super(threadName);
        this.connectionContexts = connectionContexts;
        this.setDaemon(true);
    }

    public void run() {
        this.connectionContexts.countDownLatch.countDown();

        for (; ; ) {
            // 从前面开始删除
            try {
                if (this.connectionContexts.closed) {
                    break;
                }

                if (this.connectionContexts.timeBetweenEvictionRunsMillis > 0) {
                    Thread.sleep(this.connectionContexts.timeBetweenEvictionRunsMillis);
                } else {
                    Thread.sleep(1000); //
                }

                if (Thread.interrupted()) {
                    break;
                }

                this.connectionContexts.destroyConnectionContextTask.run();
            } catch (InterruptedException e) {
                break;
            }
        }
    }

}
